/* main.vala
 *
 * Copyright (C) 2008  Frederik 'playya' Sdun
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author:
 * 	Frederik 'playya' Sdun <Frederik.Sdun@gmail.com>
 */

using GLib;
using Gtk;


public class GUIMain : Gtk.Window
{
	private Gtk.VBox main_vbox;
	private Gtk.Toolbar toolbar;
	private Gtk.Button new_button;
	private Gtk.Button delete_button;
	private Gtk.Button quit_button;
	private Gtk.ComboBox combo;
	private Gtk.Notebook notebook;
	private Gtk.VBox actions;
	private Gtk.VBox rules;
	private GLib.HashTable<string,string> configs;
	private GLib.HashTable<string,string> known_rules;
	private GLib.HashTable<string,string> known_actions;
	private string confpath;
	private string current_conf;
	private Rob.Ruleset cur_ruleset;
	private Rob.Rules.RuleFactory rf;
	private Rob.Actions.ActionFactory af;
	private Rob.UI.RuleFactory uirf;
	private Rob.UI.ActionFactory uiaf;

	bool saved;
	
	construct
	{
		this.title = "Configure Rob";
		this.destroy += Gtk.main_quit;
		this.set_default_size(480, 640);
		this.create_widgets();
		this.saved = false;
		this.configs = new GLib.HashTable<string,string>( GLib.str_hash, GLib.str_equal );
		this.known_rules = new GLib.HashTable<string,string>( GLib.str_hash, GLib.str_equal );
		this.known_actions = new GLib.HashTable<string,string>( GLib.str_hash, GLib.str_equal );
		stdout.printf("Creating Rule Factory\n");
		rf = new Rob.Rules.RuleFactory();
		stdout.printf("Creating Action Factory\n");
		af = new Rob.Actions.ActionFactory();
		stdout.printf("Creating Rule UI Factory\n");
		uiaf = new Rob.UI.ActionFactory();
		stdout.printf("Creating Action UI Factory\n");
		uirf = new Rob.UI.RuleFactory();
	}
	public void create_widgets()
	{
		main_vbox = new Gtk.VBox(false, 5);
		this.add(main_vbox);
		this.create_toolbar();
		this.create_main();
		this.add_titles();
		this.create_rules();
		this.create_actions();

		//this.create_detail_page();
		
	}
	private void add_titles()
	{
		
		string home = GLib.Environment.get_home_dir();
		confpath = home.concat("/.rob/");
		GLib.Dir dir;
		stdout.printf("Loading from: %s\n", confpath);
		try
		{
			dir = GLib.Dir.open(confpath);
		}
		catch ( GLib.FileError e )
		{
			stdout.printf("Could not open %s. Creating\n", confpath);
			GLib.DirUtils.create_with_parents(confpath,644);
			try
			{
				dir = GLib.Dir.open(confpath);
			}
			catch ( GLib.FileError e )
			{
				//should never happen
			}

		}
		for(string file = confpath + dir.read_name(); file  != null ; file = confpath + dir.read_name())
		{
			stdout.printf("Loading from %s \n", file);
			GLib.KeyFile kf = new GLib.KeyFile();
			try
			{
				string title =kf.get_string("main","title") ;
				kf.load_from_file(file,GLib.KeyFileFlags.KEEP_COMMENTS|GLib.KeyFileFlags.KEEP_TRANSLATIONS);
				this.configs.insert( title,file);
				this.combo.append_text(title);
				stdout.printf("found %s in %s\n",title, file);
			}
			catch ( GLib.KeyFileError e)
			{
				GLib.critical("Could not load config file %s: %s\n",file, e.message);	
			}
		}
		this.combo.set_active(1);
		
	}

	private void create_main()
	{
		this.notebook = new Gtk.Notebook();
		this.main_vbox.add(this.notebook);

		this.combo = new Gtk.ComboBox.text();


		Gtk.HBox tmp;
		Gtk.Label lblTitle = new Gtk.Label("Title");
		Gtk.Entry txtTitle = new Gtk.Entry();

		Gtk.VBox vbox = new Gtk.VBox(false, 5);
		this.notebook.append_page(vbox, new Gtk.Label("Main") );
		//main_page.num(this.notebook, vbox);
		//main_vbox.add(vbox);
		vbox.add_with_properties(combo, "expand", false);
		tmp = new Gtk.HBox(false, 5);
		vbox.add_with_properties(tmp,"expand", false);

		tmp.add_with_properties(lblTitle, "expand", false);
		tmp.add_with_properties(txtTitle, "expand", false);
		Gtk.CheckButton ckbActive = new Gtk.CheckButton.with_label("Active");
		vbox.add_with_properties(ckbActive,"expand", false);
		Gtk.ComboBox c = new Gtk.ComboBox.text();
		c.append_text("All of them");
		c.append_text("One of them");
		c.set_active(1);
		c.changed += combo =>
		{
			int val = combo.get_active();
			Rob.Ruleset tmp ;
			if(val == 1)
			{
				tmp = new Rob.AllRules();
				tmp.copy( this.cur_ruleset );
			}
			else if(val == 2)
			{
				tmp = new Rob.OneRule();
				tmp.copy( this.cur_ruleset );
			}
			else
			{
				GLib.critical("Wrong type for Ruleset\n");
				return;
			}
			this.cur_ruleset = tmp;

		};
		vbox.add_with_properties(c,"expand", false);
		this.add(vbox);
		//Gtk.Label lblActive = new Gtk.Label();
	}
	private void create_toolbar()
	{
		this.toolbar = new Gtk.Toolbar();
		this.main_vbox.add_with_properties(toolbar, "expand", false);
	    	this.new_button = new Gtk.Button.from_stock(Gtk.STOCK_NEW);
                this.delete_button = new Gtk.Button.from_stock(Gtk.STOCK_DELETE);
                this.quit_button = new Gtk.Button.from_stock(Gtk.STOCK_QUIT);
		
		this.quit_button.clicked += btn =>
		{
			if( main_quit_cb() == false)
				Gtk.main_quit();
		};
		this.new_button.clicked += btn => 
		{
			this.cur_ruleset = new Rob.AllRules();
			saved = false;
			show_ruleset();
		};


		this.toolbar.add( this.new_button );
		this.toolbar.add( this.delete_button );
		this.toolbar.add( this.quit_button );
		
	}
	private void show_ruleset()
	{
		this.actions = new Gtk.VBox(false, 5);
		Gtk.Toolbar rules_notebook= new Gtk.Toolbar();
		Gtk.Button add_rule = new Gtk.Button.from_stock(Gtk.STOCK_ADD);
		add_rule.clicked += btn =>
		{
				
		};
		this.title = cur_ruleset.title;
	}
	
	private void create_actions()
	{
		this.actions = new Gtk.VBox(false,5);
		this.notebook.append_page( actions, new Gtk.Label("Actions"));
	}
	private void create_rules()
	{
		this.rules = new Gtk.VBox(false,5);
		this.notebook.append_page(rules,new Gtk.Label("Rules") );
	}

	private bool main_quit_cb()
	{
		Gtk.Dialog save_dialog;
		bool ret;
		if( this.saved == false )
		{
			save_dialog = new Gtk.Dialog();
			save_dialog.set_title("Your changes haven't been saved yet");
			save_dialog.add_button(Gtk.STOCK_SAVE, ResponseType.ACCEPT);
			save_dialog.add_button (Gtk.STOCK_CANCEL, ResponseType.CANCEL);
			save_dialog.add_button (Gtk.STOCK_OK, ResponseType.OK);

			save_dialog.set_default_response(ResponseType.CANCEL);

			switch (save_dialog.run())
			{
				
				case ResponseType.ACCEPT:
					ret = false;
					break;
				case ResponseType.CANCEL:
					ret = true;
					break;
				case ResponseType.OK:
					this.save();
					ret = false;
					break;
			}
			save_dialog.close();

		}
		return ret;
	}
	public void save()
	{
		GLib.stdout.printf("saving...\n");	
	}

	static int main(string[] args)
	{

		Gtk.init( ref args );
		var gui = new GUIMain();
		Rob.init( ref args );
		gui.show_all();

		Gtk.main();
		return 0;
	}
}
